package com.zhuozhou.jdyc.welcome;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.AppManager;
import com.zhuozhou.jdyc.api.AppSettingHelper;
import com.zhuozhou.jdyc.api.UserAccountHelper;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.base.BaseViewModel;
import com.zhuozhou.jdyc.databinding.LaunchLayoutBinding;
import com.zhuozhou.jdyc.main.base.activity.FingerViewActivity;
import com.zhuozhou.jdyc.main.base.activity.LockViewActivity;
import com.zhuozhou.jdyc.main.main.activity.MainActivity;
import com.zhuozhou.jdyc.util.log.LogUtil;

import static com.zhuozhou.jdyc.api.ConstantsHelper.FINGER_VIEW_ACTIVITY_REQUEST_CODE;
import static com.zhuozhou.jdyc.api.ConstantsHelper.LOOK_VIEW_ACTIVITY_REQUEST_CODE;
import static com.zhuozhou.jdyc.api.ConstantsHelper.WELCOME_ACTIVITY_REQUEST_CODE;


/**
 * Created by 方舟 on 2017/5/15.
 * 启动页
 */
public class LaunchActivity extends BaseActivity<BaseViewModel, LaunchLayoutBinding> {
    private Handler handler = new Handler();
    private int countDown = 3;

    public BDAbstractLocationListener myListener = new MyLocationListener();
    private double mCurrentLantitude, mCurrentLongitude;
    public LocationClient mLocationClient;

    @Override
    public String setTitleBar() {
        return null;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.launch_layout;
    }

    @Override
    protected boolean hasToolBar() {
        return false;
    }


    @Override
    public void initView() {
        setThemeBarAndToolBarColor(R.color.launchColor);
    }

    @Override
    public void initData(Bundle bundle) {
        initData();
    }

    private void initData() {
//        if (AppSettingHelper.isFirstRun()) {
//            WelcomeActivity.show(this);
//        } else {

        initLocationOption();
        startCountDown();
//        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        LogUtil.show("requestCode: " + requestCode + "resultCode: " + resultCode);
        if (requestCode == WELCOME_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                startCountDown();
            }
        } else if (requestCode == LOOK_VIEW_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                MainActivity.show(this);
                finish();
            }
        } else if (requestCode == FINGER_VIEW_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                MainActivity.show(this);
                finish();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void startCountDown() {
        handler.postDelayed(runnable, 1000);
    }

    /**
     * show the LaunchActivity
     *
     * @param context context
     */
    public static void show(Context context) {
        Intent intent = new Intent(context, LaunchActivity.class);
        context.startActivity(intent);
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            countDown--;
            if (countDown == 0) {
                if (AppSettingHelper.getGesturesIsOpen() && !AppManager.isActivityTop(LockViewActivity.class, LaunchActivity.this)) {
                    LockViewActivity.show(LaunchActivity.this);
                } else if (AppSettingHelper.getFingerIsOpen() && !AppManager.isActivityTop(FingerViewActivity.class, LaunchActivity.this)) {
                    FingerViewActivity.show(LaunchActivity.this);
                } else {
                    MainActivity.show(LaunchActivity.this);
                    finish();
                }
            } else {
                handler.postDelayed(this, 1000);
            }
        }
    };


    /**
     * 初始化定位参数配置
     */

    protected void initLocationOption() {
        //定位服务的客户端。宿主程序在客户端声明此类，并调用，目前只支持在主线程中启动
        mLocationClient = new LocationClient(this.getApplicationContext());
        //声明LocationClient类实例并配置定位参数
        LocationClientOption locationOption = new LocationClientOption();
        //注册监听函数
        mLocationClient.registerLocationListener(myListener);
        //可选，设置是否需要地址信息，默认不需要
        locationOption.setIsNeedAddress(true);
        //可选，设置是否需要地址描述
        locationOption.setIsNeedLocationDescribe(true);
        mLocationClient.setLocOption(locationOption);
        //开始定位
        mLocationClient.start();
    }

    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapView 销毁后不在处理新接收的位置
            mCurrentLantitude = location.getLatitude();    //获取纬度信息
            mCurrentLongitude = location.getLongitude();    //获取经度信息
//            UserAccountHelper.saveNewCity(location.getCity());
            UserAccountHelper.saveNewCity("德州市");
            UserAccountHelper.saveCoordinates(location);
            LogUtil.show("定位成功：" + location.getCity() + mCurrentLantitude + mCurrentLongitude);
        }
    }
    @Override
    public void onStart() {
        if (!mLocationClient.isStarted()) {
            mLocationClient.start();
        }
        super.onStart();
    }

    @Override
    public void onDestroy() {
        if (mLocationClient != null)
            mLocationClient.stop();
        super.onDestroy();
    }
}
